﻿using System.Configuration;
using EmbeddedSharp.Core;
using LibUnitConverter;
using Zove.Runtime.Core.Zoom;

namespace Zove.Runtime.Core {
    public class ZoomDependencyManager {
        private static readonly ZoomDependencyManager dm = new ZoomDependencyManager();

        private IClassFinder classFinder;

        private IClassLoader classLoader;

        private IConstantManager constantManager;

        private IExpressionTranslator exprTranslator;

        private IFunctionManager functionManager;

        private IExpressionParserFactory parserFactory;

        private IRuntimeModelLoader runtimeModelLoader;

        private IUnitConverter unitConverter;

        private ZoomDependencyManager() {
        }

        public static ZoomDependencyManager Instance {
            get { return dm; }
        }

        public IConstantManager ConstantManager {
            get {
                if (constantManager == null) {
                    constantManager = new DefaultConstantManager(Utils.ConstantFile);
                }

                return constantManager;
            }
            set { constantManager = value; }
        }

        public IFunctionManager FunctionManager {
            get {
                if (functionManager == null) {
                    functionManager = new ZoomFunctionManager(new ZoomLanguageNature());
                }

                return functionManager;
            }
            set { functionManager = value; }
        }

        public IExpressionTranslator ExpressionTranslator {
            get {
                if (exprTranslator == null) {
                    exprTranslator = new DefaultExpressionTranslator();
                }

                return exprTranslator;
            }
            set { exprTranslator = value; }
        }

        public IClassFinder ClassFinder {
            get {
                if (classFinder == null) {
                    classFinder = new ZoomClassFinder();
                }
                return classFinder;
            }
            set { classFinder = value; }
        }

        public IRuntimeModelLoader RuntimeModelLoader {
            get {
                if (runtimeModelLoader == null) {
                    runtimeModelLoader = new DefaultRuntimeModelLoader(new ZoomLanguageNature());
                    //((DefaultRuntimeModelLoader)this.runtimeModelLoader).Logger = new DefaultLogger();
                    ((DefaultRuntimeModelLoader)runtimeModelLoader).ClassLoader = ClassLoader;
                }

                return runtimeModelLoader;
            }
            set { runtimeModelLoader = value; }
        }

        public IClassLoader ClassLoader {
            get {
                if (classLoader == null) {
                    classLoader = new DefaultClassLoader(new ZoomLanguageNature());
                    ((DefaultClassLoader)classLoader).Finder = ClassFinder;
                }

                return classLoader;
            }
            set { classLoader = value; }
        }

        public IExpressionParserFactory ParserFactory {
            get {
                if (parserFactory == null) {
                    parserFactory = new ZoomExpressionParserFactory(new ZoomLanguageNature());
                }
                return parserFactory;
            }
        }

        public IUnitConverter UnitConverter {
            get {
                if (unitConverter == null) {
                    unitConverter = new DefaultUnitConverter();
                }

                return unitConverter;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public static string GetFileFile(string p) {
            string filePath = ConfigurationManager.AppSettings["help"];
            filePath = filePath.EndsWith("\\") ? filePath : filePath + "\\";
            filePath = filePath + p + ".html";

            return filePath;
        }
    }
}